<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 * 文件操作类
 * @name    Cache
 * @see  
 * @version	1.0.0 (Tue Nov 04 15:19:32 CST 2008)
 * @author	sanshi
 */
class FileCache
{
	var $cache_path;
	var $io;
	function FileCache($cache_type)
	{
		$this->cache_path = CACHE_PATH."file_cache/";
		$this->io = Factory::getBaseClass("FileIO");
	}
	function __get_file_name($key)
	{
		//echo $this->cache_path.$this->io->getHashFileName($key).md5($key).".cache";
		return $this->cache_path.$this->io->getHashFileName($key).md5($key).".cache";
	}
	function put($key,$content,$over_time)
	{
		return $res = $this->io->writeFile($this->__get_file_name($key),$content);
		//jumpError(-1201);
	}
	function get($key,$over_time=60)
	{
		$cache_file = $this->__get_file_name($key);
		if($this->io->isOverTime($cache_file,$over_time))
		{
			$this->io->delFile($cache_file);
			return "";
		}
		return $this->io->getFileContent($cache_file);		
	}
	function getObject()
	{
		return $this->io;
	}
}
class EacceleratorCache
{
	var $cacheObject;
	function EacceleratorCache($cache_type)
	{
		//启用缓存
		eaccelerator_caching(true);
		//不启用php加速
		eaccelerator_optimizer(false);
	}
	function put($key,$content,$over_time=0)
	{
		return eaccelerator_put($key,$content,$over_time);
	}
	function get($key)
	{
		return $this->eaccelerator_get($key);
	}
	function getObject()
	{
		return $this;
	}
	
	function eaccelerator_cached_scripts($flag)
	{
		return eaccelerator_cached_scripts($flag);
	}
	function eaccelerator_cache_output($key,$eval_code,$ttl=0)
	{
		return eaccelerator_cache_output($key,$eval_code,$ttl=0);
	}
	function eaccelerator_cache_page($key,$ttl=0)
	{
		return eaccelerator_cache_page($key,$ttl=0);
	}
	function eaccelerator_cache_result($key,$code,$ttl=0)
	{
		return eaccelerator_cache_result($key,$code,$ttl=0);
	}
	function eaccelerator_clean()
	{
		return eaccelerator_clean();
	}
	function eaccelerator_clear()
	{
		return eaccelerator_clear();
	}
	function eaccelerator_dasm_file($filename)
	{
		return eaccelerator_dasm_file($filename);
	}
	function eaccelerator_encode ($src, $prefix = '', $pre_content = '', $post_content = '')
	{
		return  eaccelerator_encode ($src, $prefix , $pre_content , $post_content );
	}
	function eaccelerator_gc($key)
	{
		return eaccelerator_gc($key);
	}
	function eaccelerator_get($key)
	{
		return eaccelerator_get($key);
	}
	function eaccelerator_info()
	{
		return eaccelerator_info();
	}
	function eaccelerator_list_keys()
	{
		return eaccelerator_list_keys();
	}
	function eaccelerator_load()
	{
		return eaccelerator_load();
	}
	function eaccelerator_lock($key)
	{
		return eaccelerator_lock($key);
	}
	function eaccelerator_purge()
	{
		return eaccelerator_purge();
	}
	function eaccelerator_removed_scripts()
	{
		return eaccelerator_removed_scripts();
	}
	function eaccelerator_rm($key)
	{
		return eaccelerator_rm($key);
	}
	function eaccelerator_rm_page($key)
	{
		return eaccelerator_rm_page($key);
	}
	function eaccelerator_unlock($key)
	{
		return eaccelerator_unlock($key);
	}
}
class MemcacheCache
{
	function MemcacheCache($cache_type)
	{

	}
	function put()
	{}
	function get()
	{}
	function getObject()
	{}
}
class TTCache
{
	var $con_arr = array();
	var $tt_object_read;
	var $tt_object_write; 
	var $key_prefix;
	function TTCache($cache_type)
	{
		global $cache_con_arr;
		$this->con_arr = $cache_con_arr[$cache_type];
		$this->key_prefix = $this->con_arr['key'];
	}
	function connect($con_type)
	{
		$object_str = "tt_object_{$con_type}";
		if(!is_object($this->$object_str))
		{
			$this->$object_str = new Memcached();
			$this->$object_str->addServers($this->con_arr[$con_type]);
	    }
		return $this->$object_str;
	}
	function get_key($key)
	{
		return $this->key_prefix."_".$key;
	}
	function put_arr($key,$content,$over_time=0)
	{
		$this->put($key,$content,$over_time);
	}
	function put($key,$content,$over_time=0)
	{
		$object = $this->connect('write');
		$object->set($this->get_key($key),$content);
	}
	function get($key,$over_time=0)
	{
		$object = $this->connect('read');
		return $object->get($this->get_key($key));
	}
	function get_arr($key,$over_time=0)
	{
		return unserialize($this->get($key,$over_time));
	}
	function del($key)
	{
		$object = $this->connect('write');
		$object->delete($this->get_key($key));
	}
	function getObject()
	{
		$object = $this->connect('write');
		return $object;
	}
}

class Cache
{
	function Cache()
	{
		//介质 存储介质
		$this->media_type = array(
			'file'=>'FileCache',
			'php'=>'EacceleratorCache',
			'tt'=>'TTCache',
			'mc'=>'MemcacheCache',
			);
		//缓存类型
		$this->cache_type = array(
			'zl' =>'zl',
		    'topic' => 'topic',
		    'mlt' => 'mlt',
		    'fc' => 'fc',
		    'mc' => 'mc',
		    'yz' => 'yz',
		    'jzq' => 'jzq',
		    'mfc' => 'mfc',
		    'dfc' => 'dfc',
		    'mec' => 'mec',
		    'user' => 'user',
		    'cww' => 'cww',
		    'ml' => 'ml',
		    'ht' =>'ht',
		    );
	}
	function set($media_type,$cache_type)
	{
		//echo $this->media_type[$media_type]."<br/>".$this->cache_type[$cache_type];
		if( isset($this->media_type[$media_type]) && isset($this->cache_type[$cache_type]))
		{
			//$this->media_type[$media_type]($this->cache_type[$cache_type]);
			return new $this->media_type[$media_type]($this->cache_type[$cache_type]);
		}
				
	}
}

/*
$temp = new cache();
$c =$temp->set('tt','zl');
$c->put_arr("sanshi",array("sa"=>'我来了'));
print_r($c->get_arr("sanshi"));
$c->put("sa","asdfsdf");
print_r($c->get('sa'));
*/
?>